//
//  FRPFullSizePhotoViewController.m
//  FunctionalReactivePixels
//
//  Created by Ran on 15/9/7.
//  Copyright (c) 2015年 gintong. All rights reserved.
//

#import "FRPFullSizePhotoViewController.h"
#import "FRPPhotoModel.h"
#import "FRPPhotoViewController.h"
#import "FRPFullSizePhotoViewModel.h"

@interface FRPFullSizePhotoViewController ()<UIPageViewControllerDataSource, UIPageViewControllerDelegate>


@property(nonatomic, strong)UIPageViewController *pageViewController;

@property(nonatomic, strong)FRPFullSizePhotoViewModel *viewModel;

@end

@implementation FRPFullSizePhotoViewController

- (instancetype)initWithPhotoModels:(NSArray *)photoModelArray currentPhotoIndex:(NSInteger)photoIndex
{
    
    self = [self init];
    if (!self) {
        return nil;
    }
    
    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionInterPageSpacingKey: @30}];
    self.pageViewController.dataSource = self;
    self.pageViewController.delegate = self;
    [self addChildViewController:self.pageViewController];
    self.pageViewController.view.backgroundColor = [UIColor blackColor];
    
    return self;
    
}

- (FRPPhotoViewController *)photoViewControllerForIndex: (NSInteger)index{

    if (index >=0 && index < self.viewModel.model.count) {
        FRPPhotoModel *model = self.viewModel.model[index];
        FRPPhotoViewController *photoViewController = [[FRPPhotoViewController alloc] initWithPhotoModel:model photoIndex:index];
        return photoViewController;
    }
    return nil;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];
    self.pageViewController.view.frame = self.view.bounds;
    
    [self.pageViewController setViewControllers:@[[self photoViewControllerForIndex:self.viewModel.initialPhotoIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    self.title = self.viewModel.initialPhotoName;
    
    [self.view addSubview:self.pageViewController.view];
    
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
    
    self.title = [[self.pageViewController.viewControllers.firstObject photoModel] t];
    [self.delegate userDidScroll:self toPhotoAtIndex:[self.pageViewController.viewControllers.firstObject photoIndex]];

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(FRPPhotoViewController *)viewController{
    return [self photoViewControllerForIndex:viewController.photoIndex - 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(FRPPhotoViewController *)viewController{
    return [self photoViewControllerForIndex:viewController.photoIndex + 1];

}


@end










